package kin.system.util;
 import  java.io. * ;
 import  java.util. * ;
 /** 
 *   Put files into a tree
 **/ 
  public   class  ReadFilesAndBuiltTree   {
    
     static   class  Node  {
         private  Node father;
         private  List children;
         private  File data;
         public  Node()  {
            children  =   new  Vector();
        } 

     public  File getData()   {
             return  data;
        } 

          public   void  addChild(Node c)  {
             this .getChildren().add(c);
        } 
          public   void  setData(File data)   {
             this .data  =  data;
        } 
          public  List getChildren()   {
             return  children;
        } 
          public   void  setChildren(List children)   {
             this .children  =  children;
        } 
          public  Node getFather()   {
             return  father;
        } 
          public   void  setFather(Node father)   {
             this .father  =  father;
        } 
          public   boolean  hasChildren()  {
             return  children.size() > 0 ;
        } 
          public  Node getChile( int  index)  {
             return  (Node)children.get(index);
        } 
        


  public  Node getChild(String fname)  {
            Iterator i  =  getChildren().iterator();
             while (i.hasNext())  {
                Node bn  =  (Node)i.next();
                String n  =  
                    bn.getData().getName();
                 if (n.equals(fname)) return  bn;
            } 
             return   null ;
        } 
          public  List getDirChildren()  {
            List c  =   new  Vector();
             for ( int  i  =   0  ; i  < children.size();i ++ )  {
                Node n  =  (Node)children.get(i);
                 if (
                  n.getData().isDirectory()        
                )c.add(n);
            } 
             return  c;
        } 
          public  List getFileChildren()  {
            List c  =   new  Vector();
             for ( int  i  =   0  ; i  < children.size();i ++ )  {
                Node n  =  (Node)children.get(i);
                 if (
                   ! n.getData().isDirectory()        
                )c.add(n);
            } 
             return  c;
        } 
    } 
    
     private  ReadFilesAndBuiltTree()  {} 
    
     public   static  Node getTree(File rootDir)  {
        Node r  =   new  Node();
        r.setData(rootDir);
         return   new  ReadFilesAndBuiltTree().builtTree(r);
    } 
    
     private  Node builtTree(Node root)  {
         if ( ! root.getData().isDirectory()) return  root;
        File f  =  root.getData();
        File[] fs  =  f.listFiles();
         for ( int  i  =   0  ; i  <  fs.length ; i ++  )  {
            File ff  =  fs[i];
            Node n  =   new  Node();
            n.setData(ff);
            n.setFather(root);
            root.addChild(builtTree(n));
        } 
         return  root;
    } 
    
  /*public   static   void  main(String[] args) throws  Exception  {
        Node root  =  getTree( new  File(ShowFileDir.ROOT_DIR));
        Node cn  =  root.getChild( "maps" );
        System.out.println(cn.getChildren().size());
    } */
    
} 